package com.naizhuai.ui.home;

import android.support.annotation.Nullable;

import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.CommonManagerApiStore;
import com.naizhuai.network.apistore.apistoreiml.CommonManagerIml;
import com.naizhuai.ui.entity.MainPage;
import com.naizhuai.ui.entity.RecommendScheme;

import java.util.List;

/**
 * @author：lichenxi
 * @date 2018/1/26 15
 * email：525603977@qq.com
 * Fighting
 */
public class HomePresenter implements HomeContract.Presenter {
    private CommonManagerApiStore mManagerApiStore;
    private int page;

    public HomePresenter() {
        mManagerApiStore = CommonManagerIml.getInstance();
    }

    @Nullable
    private HomeContract.View mHomeView;


    @Override
    public void takeView(HomeContract.View view) {
        this.mHomeView = view;
//        loadMainPage();
    }

    @Override
    public void dropView() {
        mHomeView = null;
    }

    @Override
    public void loadMainPage() {
        page = 0;
        mManagerApiStore.getMainPage()
                .subscribe(new NetWorkObserver<MainPage>() {
                    @Override
                    protected void onFail(String errormsg) {
                        mHomeView.showFailMsg(errormsg);
                        mHomeView.stopRefresh();
                    }
                    @Override
                    protected void onSuccess(MainPage result) {
                        mHomeView.showNotice(result.notice);
                        mHomeView.showBanner(result.bannerList);
                        mHomeView.showBirghtPoints(result.recommendScheme, false);
                        mHomeView.stopRefresh();
                    }
                });
    }

    @Override
    public void loadMoreBirghtPoint() {
        mManagerApiStore.getRecommendScheme(++page)
                .subscribe(new NetWorkObserver<List<RecommendScheme>>() {
                    @Override
                    protected void onFail(String errormsg) {
                        mHomeView.showFailMsg(errormsg);
                        mHomeView.stopRefresh();
                    }
                    @Override
                    protected void onSuccess(List<RecommendScheme> result) {
                        mHomeView.showBirghtPoints(result, true);
                        mHomeView.stopRefresh();
                    }
                });
    }
}
